/**
 * 列表
 */
import { Checkbox, message } from 'antd';
import an58 from '@mmstudio/an000058';
import Table from '../../../components/table';
import { IData } from '../../api/class/add/query.api';
import api from '../../../atoms/api';
import { Message as M1, Result as R1 } from '../../api/class/add/list/add.api';

export default function List({ id, data, selected, onChange }: {
	data: IData[];
	id: string;
	selected: string[];
	onChange(list: string[]): void;
}) {
	return <>
		<Table
			idfieldname='student_id'
			data={data}
			render={(record) => {
				const student_id = record.student_id;
				const exist = selected.find((v) => {
					return v === student_id;
				});
				return <div className='op' key={student_id}>
					<Checkbox checked={Boolean(exist)} onChange={async (e) => {
						if (e.target.checked) {
							const ret = await an58<R1, M1>(api['/api/class/add/list/add'], 'post', {
								class_id: id,
								student_id




							});
							if (ret.ok === false) {
								message.error(ret.message);
								return;
							}
							// const abc = await an58<R1, M1>(api.class.add.list.del, 'post', {
							// 	classnumber: id,
							// 	student_id




							// });
							// if (abc.ok === false) {
							// 	message.error(abc.message);
							// 	return;
							// }
							const exist = selected.find((v) => {
								return v === student_id;
							});
							if (!exist) {
								onChange([...selected, student_id]);
							}
						} else {
							const abc = await an58<R1, M1>(api['/api/class/add/list/del'], 'post', {
								class_id: id,
								student_id




							});
							if (abc.ok === false) {
								message.error(abc.message);
								return;
							}
							const list = selected.filter((it) => {
								return it != student_id;
							});
							onChange(list);
						}
					}} >加入班级</Checkbox>
				</div>;
			}}
		>
			<Table.Column<IData> title='姓名' key='name' dataIndex='name' align='center' />
			<Table.Column<IData> title='性别' key='gender' dataIndex='gender' align='center' />
			<Table.Column<IData> title='学号' key='student_id' dataIndex='student_id' align='center' />
			<Table.Column<IData> title='学院' key='college' dataIndex='college' align='center' />
			<Table.Column<IData> title='专业' key='originalmajor' dataIndex='originalmajor' align='center' />
			<Table.Column<IData> title='班级' key='originalclass' dataIndex='originalclass' align='center' />
			<Table.Column<IData> title='政治面貌' key='politicaloutlook' dataIndex='politicaloutlook' align='center' />
			<Table.Column<IData> title='联系电话' key='telephone' dataIndex='telephone' align='center' />
			<Table.Column<IData> title='意向专业' key='intendedmajor' dataIndex='intendedmajor' align='center' />
			<Table.Column<IData> title='专业名称' key='majorname' dataIndex='majorname' align='center' />
			<Table.Column<IData> title='班级名称' key='classname' dataIndex='classname' align='center' />
		</Table>
	</>;
}

